package com.ruoyi.smartcity.huanjing.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.smartcity.huanjing.domain.HuanjingZaosheng;
import com.ruoyi.smartcity.huanjing.mapper.HuanjingZaoshengMapper;
import com.ruoyi.smartcity.huanjing.service.IHuanjingZaoshengService;
import com.ruoyi.smartcity.traffic.vo.EchartsOptionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 噪声Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class HuanjingZaoshengServiceImpl implements IHuanjingZaoshengService {
    @Autowired
    private HuanjingZaoshengMapper huanjingZaoshengMapper;

    /**
     * 查询噪声
     *
     * @param id 噪声主键
     * @return 噪声
     */
    @Override
    public HuanjingZaosheng selectHuanjingZaoshengById(Long id) {
        return huanjingZaoshengMapper.selectHuanjingZaoshengById(id);
    }

    /**
     * 查询噪声列表
     *
     * @param huanjingZaosheng 噪声
     * @return 噪声
     */
    @Override
    public List<HuanjingZaosheng> selectHuanjingZaoshengList(HuanjingZaosheng huanjingZaosheng) {
        return huanjingZaoshengMapper.selectHuanjingZaoshengList(huanjingZaosheng);
    }

    /**
     * 新增噪声
     *
     * @param huanjingZaosheng 噪声
     * @return 结果
     */
    @Override
    public int insertHuanjingZaosheng(HuanjingZaosheng huanjingZaosheng) {
        huanjingZaosheng.setCreateTime(DateUtils.getNowDate());
        return huanjingZaoshengMapper.insertHuanjingZaosheng(huanjingZaosheng);
    }

    /**
     * 修改噪声
     *
     * @param huanjingZaosheng 噪声
     * @return 结果
     */
    @Override
    public int updateHuanjingZaosheng(HuanjingZaosheng huanjingZaosheng) {
        huanjingZaosheng.setUpdateTime(DateUtils.getNowDate());
        return huanjingZaoshengMapper.updateHuanjingZaosheng(huanjingZaosheng);
    }

    /**
     * 批量删除噪声
     *
     * @param ids 需要删除的噪声主键
     * @return 结果
     */
    @Override
    public int deleteHuanjingZaoshengByIds(Long[] ids) {
        return huanjingZaoshengMapper.deleteHuanjingZaoshengByIds(ids);
    }

    /**
     * 删除噪声信息
     *
     * @param id 噪声主键
     * @return 结果
     */
    @Override
    public int deleteHuanjingZaoshengById(Long id) {
        return huanjingZaoshengMapper.deleteHuanjingZaoshengById(id);
    }

    @Override
    public EchartsOptionVO zaosheng() {
        EchartsOptionVO echartsOptionVO = new EchartsOptionVO();
        List<HuanjingZaosheng> list = huanjingZaoshengMapper.selectHuanjingZaoshengList(new HuanjingZaosheng());
        if (list != null && !list.isEmpty()) {
            // 分组 name
            JSONArray yData = new JSONArray();
            JSONArray xData = new JSONArray();
            for (HuanjingZaosheng item : list) {
                xData.add(item.getName());
                yData.add(item.getMonitorValue());
                echartsOptionVO.setXData(xData);
                echartsOptionVO.setYData(yData);
            }
        }
        return echartsOptionVO;
    }
}
